<?php
/**
* @version		$Id: dynl_data.php 1 2007-01-11 22:03:46Z friesengeist $
* @package		Dynlist
* @copyright	Copyright (C) 2005 - 2007 Dynlist. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Dynlist is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
/**
* Dynlist data class
*
* This class manages the data of Dynlist.
*
* @package		Dynlist
* @final
*/
class Dynl_data
{
	/**
	 * Dynlist name.
	 *
	 * @var char
	 * @access protected
	 */
	var $_dynlname = null;
	
	/**
	 * Database instance.
	 *
	 * @var object db_catalog object
	 * @access private
	 */
	var $_database;
	
	/**
	 * HTML instance.
	 *
	 * @var object html_catalog object
	 * @access private
	 */
		var $_html;

	/**
	 * Key fields list
	 * 
	 * @var array
	 * @access protected
	*/
	var $_keyfields = null;
	
	/**
	 * Dynlist data.
	 *
	 * @var array
	 * @access protected
	 */
	var $_data = null;

	/**
	 * Layout used in actual Dynlist (for screen generator).
	 *
	 * @var object  Layout object
	 * @access protected
	 */
	var $_layout = null;
	
	/**
	 * Sctructure used to print headers.
	 *
	 * @var object Dynl_Structure object
	 * @access protected
	 */
	var $_structure = null;
	
	/**
	* Class constructor
	*
	* @access public
	* @param char $dynlname
	*/
	function Dynl_data($dynlname)
	{
		$this->_dynlname = $dynlname;
		$this->_database = new dynl_database($dynlname);
		$this->_html = new html_data();
		$this->_structure = new dynl_structure($dynlname);
		
		$this->_keyfields = $this->_structure->get_keyfields();
		$this->_html->set_dynlname($this->_dynlname);
	}

	/**
	* Invocated HTML render.
	*
	* @access public
	*/
	function show_data()
	{
		//echo '<a href="javascript:history.go(-1)">Voltar</a>';
		
		// Retrieve data from database.
		$this->_data = $this->_database->retrieve('all','all','none');
		
		// Set keyvalues into _data attribute.
		$this->agregate_keyvalues();

		// Set dynlist header texts.
		$this->_html->set_header($this->_structure->get_texts());
		
		// Set keys fields.
		$this->_html->set_keyfields($this->_keyfields);
		//echo 'keys:';
		//print_r($this->_keyfields);
			
		// Set page data;
		$this->_html->set($this->_data);
		
		// Render page;
		$this->_html->render_page();
		
	}

	/**
	* Manage the creation of new record.
	*
	* @access public
	*/
	function new_record()
	{
		//echo '<a href="javascript:history.go(-1)">New Record</a>';
		
		// parse keys values
		//print_r($key_char);	
		
		// Create dynl_record object.
		$record = new dynl_datarec($this->_dynlname);
		//echo 'keys--------->';
		//print_r($this->get_keyindexes());
		
		// Set key indexes
		$record->set_keyindexes($this->_structure->get_keyindexes());
		
		// Edit record
		$record->new_record();
	}

	/**
	* Manage the creation of new record.
	*
	* @access public
	* @param char $key_char
	*/
	function edit_record($key_char)
	{
		//print_r($key_char);	
		
		// Create dynl_record object.
		$record = new dynl_datarec($this->_dynlname);
		//echo 'keys--------->';
		//print_r($this->get_keyindexes());
		
		// Set key indexes
		$record->set_keyindexes($this->_structure->get_keyindexes());
		
		// Edit record
		$record->edit_record($key_char);
	}

	/**
	* Delete intire record.
	*
	* @access public/
	* @param char $fieldvalues
	*/
	function delete_record($key_char)
	{
		//print_r($key_char);	
		
		// Create dynl_record object.
		$record = new dynl_datarec($this->_dynlname);
		//echo 'keys--------->';
		//print_r($this->get_keyindexes());
		
		// Set key indexes
		$record->set_keyindexes($this->_structure->get_keyindexes());
		
		// Edit record
		$record->delete_record($key_char);
		
		//print_r($key_char);
		
		//$this->_database->delete($this->get_array_fieldvalues($key_char));
		
		// Back to list.
		echo '<script type=""text/javascript"">window.location = "/dynlist/index.php?xcode=data!'.$this->_dynlname.'!show_data"</script>';
	}
	
	/**
	* Save record on database
	*
	* @access public
	* @param char $char_fieldvalues
	*/
	function save_record($char_fieldvalues)
	{
		
		// Convert char_fieldvalues to array field values.
		$setvalues = $this->get_array_fieldvalues($char_fieldvalues);
		
		//echo 'char_fieldvalues--->'.$char_fieldvalues."\n";
		//echo 'setvalues--->';
		//print_r($setvalues);
		
		// Save record
		$this->_database->update($this->_structure->get_keyfields(),$setvalues);

		// Back to list.
		echo '<script type=""text/javascript"">window.location = "/dynlist/index.php?xcode=data!'.$this->_dynlname.'!show_data"</script>';
	}
	
	/**
	* Ignore all changes on record and return to data list.
	*
	* @access public
	*/
	function cancel()
	{
		// Back to list.
		echo '<script type=""text/javascript"">window.location = "/dynlist/index.php?xcode=data!'.$this->_dynlname.'!show_data"</script>';
	}

        /**
         * Agregate key values into _data attribute.
	 * This method include one item at end of _data array with key values for the line.
         *
         * @access proteced
        */
        function agregate_keyvalues()
        {	
		// Include one item at header for keyvalues.
		array_push($this->_data[0],'keyvalues');
		
		// Get keyindexes.
		$keyindexes = $this->_structure->get_keyindexes();
		//echo 'keyindexes --->';
		//print_r($keyindexes);
		
		for ($i = 1; $i < count($this->_data); $i++)
		{
			// Data record
			$h_data = $this->_data[$i];

			for ($j = 0; $j <= count($h_data); $j++)
			{
				if (in_array($j, $keyindexes))
				{
					if ($j <> 0) { 
						$keyvalue .= ';';
					}
					$keyvalue .= $h_data[$j];
					 
				}
			}
			
			array_push($h_data,$keyvalue);
			$keyvalue = null;
			$this->_data[$i] = $h_data;
		
		}
        }

        /**
         * Convert char_fieldvalues to array field values.
         *
         * @access proteced
         */
	function get_array_fieldvalues($char_fieldvalues)
        {

		// Initialize array setvalues.
		$setvalues = array();
		
		// Convert char_fieldvalues to array field values.
		$fieldvalues = spliti(";",$char_fieldvalues);
		foreach ($fieldvalues as $fieldvalue)
		{
			$setvalue = spliti("~",$fieldvalue);
			array_push($setvalues,$setvalue);
		}

		return $setvalues;
	}
	
        /**
         * Return a list of all index of fields that are part of key.
         *
         * @access proteced
         */
        /*
	function get_keyindexes()
        {
                // Initialize $indexes array.
                // This array has the list of all index fields that are keys.
                $indexes = array();
		echo 'data------->';
		print_r($this->_data[0]);
                // Get index in line array at data array for fields that are keys.
                foreach ($this->_data[0] as $fieldname)
                {
			// Index of actual field.
			$index = array_search($fieldname,$this->_keyfields);
			
			// If actual field name is a interger, it means that it's a key.
			if (is_int($index))
		       	{
				// Include fieldname in indexes array.
				array_push($indexes,$index);
			}
		}
		echo 'indexes----->';
		print_r($indexes);
		return $indexes;
	}
	*/
}
?>
